package com.draw.servlet;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

public class DrawArcServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //禁止页面缓存
        resp.setHeader("Pragma", "No-cache");
        resp.setHeader("Cache-Control", "No-cache");
        resp.setDateHeader("Expires", 0);
        resp.setContentType("image/JPEG");//设置响应格式为JPEG图片
        //创建一个指定长度的图像
        int width = 260, height = 180;
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
        Graphics graphics = image.getGraphics();//获取用于处理图形上下文的对象
        graphics.setColor(new Color(221, 221, 221));//设置第一个矩形的RGB颜色
        graphics.fillRect(0, 0, 260, 180);//设置实心矩形
        graphics.setColor(Color.BLACK);//设置颜色
        //绘制圆弧参数说明如下：
        //要绘制的左上角的x坐标，要绘制的左上角的y坐标
        //要绘制圆弧的宽度，要绘制圆弧的高度
        //开始角度，相对于来时角度而言，弧跨越的角度
        graphics.drawArc(20,20,80,80,0,120);
        graphics.drawArc(20,40,80,80,0,-120);
        graphics.drawArc(150,20,80,80,180,-120);
        graphics.drawArc(150,40,80,80,180,120);
        graphics.dispose();//释放次图形的上下文以及它使用的所有系统资源
        ImageIO.write(image, "JPEG", resp.getOutputStream());//输出JPEG格式图片到页面
        resp.getOutputStream().flush();//刷新输出流
        resp.getOutputStream().close();//关闭输出流
    }
}
